package com.android.UniPlugin;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.provider.Settings;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.dcloud.application.DCloudApplication;


public class MyApplication extends DCloudApplication {
    private PowerManager powerManager;
    private PowerManager.WakeLock wakeLock;
    @Override
    public void onCreate() {
        super.onCreate();

        /**/
        // 在这里初始化全局状态或第三方库
        // 获取 PowerManager 服务
        powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);

        // 创建一个 WakeLock，保持屏幕常亮
        wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "MyApp::KeepScreenOn");

        // 获取 WakeLock
        wakeLock.acquire();

        // 注册每个 Activity 的生命周期监听
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
                // 在 Activity 创建时设置沉浸式模式
                hideSystemUI(activity);
            }

            @Override
            public void onActivityStarted(Activity activity) {}
            @Override
            public void onActivityResumed(Activity activity) {
                hideSystemUI(activity);
            }

            @Override
            public void onActivityPaused(Activity activity) {}
            @Override
            public void onActivityStopped(Activity activity) {}
            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}
            @Override
            public void onActivityDestroyed(Activity activity) {}

            private void hideSystemUI(Activity activity) {
                //需要运行全屏代码的设备列表（运行会有切屏后，输入框顶部不透明的问题）
                List<Map<String, String>> needHideNavDeviceInfoList = new ArrayList<>();
                Map<String, String> map = new HashMap<>();
                map.put("brand", "HUAWEI");//制造商  HUAWEI
                map.put("device_model","AGS6-W10");//设备名称（型号代码）    AGS6-W10
                map.put("system_version","10");//安卓系统版本   10
                map.put("zengliang_version","103.0.0.110C00");//增量版本号   103.0.0.110C00
                needHideNavDeviceInfoList.add(map);

                //APP切后台回来后修复底部导航显示
                //显示当前设备的机型和系统版本
                String brand= Build.MANUFACTURER;//制造商  HUAWEI
                String device_model=Build.MODEL;//设备名称（型号代码）    AGS6-W10
                String system_version=Build.VERSION.RELEASE;//安卓系统版本   10
                String zengliang_version=Build.VERSION.INCREMENTAL;//增量版本号   103.0.0.110C00

                Boolean isNeedHideNav = false;
                for(Map<String, String> needHideNavDeviceInfo : needHideNavDeviceInfoList){
                    
                    if((isEmpty(needHideNavDeviceInfo.get("brand"))||(!isEmpty(needHideNavDeviceInfo.get("brand")) &&needHideNavDeviceInfo.get("brand").equals(brand)))
                        &&(isEmpty(needHideNavDeviceInfo.get("device_model"))||(!isEmpty(needHideNavDeviceInfo.get("device_model")) && needHideNavDeviceInfo.get("device_model").equals(device_model)))
                        &&(isEmpty(needHideNavDeviceInfo.get("system_version"))||(!isEmpty(needHideNavDeviceInfo.get("system_version")) && needHideNavDeviceInfo.get("system_version").equals(system_version)))
                        &&(isEmpty(needHideNavDeviceInfo.get("zengliang_version"))||(!isEmpty(needHideNavDeviceInfo.get("zengliang_version")) && needHideNavDeviceInfo.get("zengliang_version").equals(zengliang_version)))){
                        isNeedHideNav = true;
                        break;
                    }
                }
//                String deviceInfo =
//                    "制造商："+brand+
//                    "\n设备名称: " + device_model +
//                    "\n系统版本号: " + system_version+
//                    "\n增量版本号:"+zengliang_version;
//                new android.app.AlertDialog.Builder(activity)
//                        .setTitle("设备信息")
//                        .setMessage(deviceInfo)
//                        .setPositiveButton("确定", null)
//                        .show();

                //判断机型
                if(isNeedHideNav){
//                    new android.app.AlertDialog.Builder(activity)
//                       .setTitle("提示")
//                       .setMessage("运行全屏")
//                       .setPositiveButton("确定", null)
//                       .show();
                    View decorView = activity.getWindow().getDecorView();
                    decorView.setSystemUiVisibility(
                            View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    );
                }
            }
        });

    }

    /**
     * 判断字符串是否为空
     */
    private boolean isEmpty(String value){
        return value == null || value.isEmpty();
    }

    @Override
    public void onTerminate() {
        super.onTerminate();

        // 释放 WakeLock
        if (wakeLock != null && wakeLock.isHeld()) {
            wakeLock.release();
        }
    }
}